package com.haois.ws.common.security;


import com.haois.ws.common.constant.CommonConstants;
import com.haois.ws.common.enums.ApiResultEnum;
import com.haois.ws.common.exception.OutException;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2018/3/10 19:40
 */
public class DataCheck {

    public static void codeNot0X(Map result) throws OutException {
        String code = (String) result.get("code");
        if (!code.startsWith("0")) {
            throw new OutException(ApiResultEnum.API_16.getCode(), ApiResultEnum.API_16.getMsg());
        }
    }

    /**
     * 数据服务异常
     *
     * @param result
     * @throws OutException
     */
    public static void codeIs16(Map result) throws OutException {
        if (ApiResultEnum.API_16.getCode().equals(result.get("code"))) {
            throw new OutException(ApiResultEnum.API_16.getCode(), ApiResultEnum.API_16.getMsg());
        }
    }

    public static String getOpenId(HttpServletRequest request){
        try {
            return (String) request.getSession().getAttribute(
                    CommonConstants.OPENID_KEY);
        }catch (Exception e){
            return "";
        }

    }
}
